// ignore_for_file: depend_on_referenced_packages
import 'package:animation/components/AlertDialog/my_loading.dart';
import 'package:animation/components/appbar_base.dart';
import 'package:animation/components/images/image_load.dart';
import 'package:animation/model/play_video_base.dart';
import 'package:animation/pages/other/videos/video_play.dart';
import 'package:animation/tools/route_animation.dart';
import 'package:flutter/material.dart';
import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart' show Document;

class NunoPage extends StatefulWidget {
  final String title;
  final String cover;
  final String desc;
  final String videoList;
  final int line;
  const NunoPage({
    super.key,
    required this.title,
    required this.cover,
    required this.desc,
    required this.videoList,
    required this.line,
  });

  @override
  State<NunoPage> createState() => _NunoPageState();
}

class _NunoPageState extends State<NunoPage> {
  bool isLoading = false;
  List<PlayVideoBase> videoList = [];

  void initData() {
    setState(() {
      isLoading = true;
    });
    try {
      if (!mounted) return;
      List<String> videoListStr = widget.videoList.split("\$\$\$");
      List videos1 = widget.line == 1
          ? videoListStr[0].split("#")
          : videoListStr[1].split("#");
      for (var i = 0; i < videos1.length; i++) {
        var video = videos1[i].split("\$");
        videoList.add(PlayVideoBase(
          title: video[0],
          long_title: video[0],
          url: video[1],
        ));
      }
    } catch (e) {
      print(e);
    } finally {
      setState(() {
        isLoading = false;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    initData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppbarBase(title: widget.title),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          children: [
            SizedBox(
              height: 200,
              child: Row(
                spacing: 10,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(5),
                    child: Hero(
                      tag: 'widget.ssid',
                      child: SizedBox(
                        // 宽度为屏幕宽度
                        width: 150,
                        height: 200,
                        child: ImageLoad(imageUrl: widget.cover),
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      height: 200,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(5),
                          color: Theme.of(context).colorScheme.primaryContainer,
                          boxShadow: [
                            BoxShadow(
                              color: Theme.of(context).colorScheme.shadow,
                              offset: Offset(1, 1),
                              blurRadius: 1,
                            ),
                          ]),
                      child: isLoading
                          ? Center(
                              child: MyLoading(
                                type: 2,
                              ),
                            )
                          : Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: ListView(
                                children: [
                                  Text(_parseHtml(widget.desc)),
                                ],
                              ),
                            ),
                    ),
                  )
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                IconButton(
                    onPressed: () {
                      setState(() {
                        // 数组倒序
                        videoList = videoList.reversed.toList();
                      });
                    },
                    icon: Icon(Icons.import_export)),
                IconButton(onPressed: () {}, icon: Icon(Icons.favorite_border)),
              ],
            ),
            Expanded(
                child: isLoading
                    ? Center(
                        child: MyLoading(
                          type: 2,
                          size: 50,
                        ),
                      )
                    : GridView.builder(
                        itemCount: videoList.length,
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 3,
                          crossAxisSpacing: 8,
                          mainAxisSpacing: 8,
                          childAspectRatio: 3,
                        ),
                        itemBuilder: (context, index) {
                          return Material(
                            color:
                                Theme.of(context).colorScheme.primaryContainer,
                            borderRadius: BorderRadius.circular(5),
                            shadowColor: Theme.of(context).colorScheme.shadow,
                            clipBehavior: Clip.antiAlias,
                            elevation: 2,
                            child: InkWell(
                              onTap: () {
                                RouteAnimation().animateRoute(
                                  context,
                                  VideoPlay(
                                    videoList: videoList,
                                    activeIndex: index,
                                    title: widget.title,
                                  ),
                                );
                              },
                              child: Center(
                                child: Text(
                                  videoList[index].title,
                                ),
                              ),
                            ),
                          );
                        })),
          ],
        ),
      ),
    );
  }

  String _parseHtml(String htmlContent) {
    // 解析 HTML
    Document document = parse(htmlContent);
    // 提取纯文本
    String text = document.body?.text ?? '';
    // 替换 HTML 中的换行符
    text = text.replaceAll('\n', '\n\n');
    return text;
  }
}
